package net.oschina.arvin.dbjava.model;

/**
 * Import 描述符
 *
 * @author Arvin
 * @time 2017/9/29 17:16
 */
public class ImportDescriptor {

    /** 导入代码 */
    private final String importCode;

    /** 是否静态导入 */
    private final boolean staticImport;

    /** 导入的class， 只有非.*导入才有 */
    private final Class<?> importClass;

    public ImportDescriptor(String importCode) {
        this.importCode = importCode;
        this.staticImport = importCode.matches("(?i)^[\\s]*import[\\s]+static.*$");
        if (!this.staticImport && !importCode.contains("*")) {
            String className = importCode.replaceAll("(?i)[\\s]*import[\\s]+(.*);[\\s]*$", "$1").trim();
            try {
                this.importClass = Class.forName(className);
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
        } else {
            this.importClass = null;
        }
    }

    public String getImportCode() {
        return importCode;
    }

    public boolean isStaticImport() {
        return staticImport;
    }

    public Class<?> getImportClass() {
        return importClass;
    }
}
